//
//  MGChartViewController.m
//  MGSplitView
//
//  Created by Matt Gemmell on 26/07/2010.
//  Copyright Instinctive Code 2010.
//

#import "MGChartViewController.h"
#import "MGTreeViewController.h"
#import "CorePlot-CocoaTouch.h"

@interface MGChartViewController ()

 
- (void)configureView;

@end


@implementation MGChartViewController


@synthesize toolbar , detailItem, detailDescriptionLabel,splitController,ccchart;

#pragma mark - 載入畫面
#pragma mark 

-(void)loadView{
    NSLog(@"viewDidLoad MGChartViewController");    
    
    //視窗
    UIView *dv=[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    dv.backgroundColor=[UIColor whiteColor];
     
    //工具列
    {
        toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 1024, 44)];    
        
       
      
        UIBarButtonItem_toggleMasterView = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"TriangleSmall2.png"] style:UIBarButtonItemStylePlain target:self action:@selector(toggleMasterView:)];
        UIBarButtonItem_toggleMasterView.title=@"dd";
        
        //UIBarButtonItem_toggleMasterView = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"MasterView",@"Show Controller") style:UIBarButtonItemStyleBordered target:self action:@selector(toggleMasterView:)];         
        
        UIBarButtonItem_toggleVertical = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Vertical",@"Show Controller") style:UIBarButtonItemStyleBordered target:self action:@selector(toggleVertical:)];    
        
        UIBarButtonItem_toggleDividerStyle = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"MasterView",@"Show Controller") style:UIBarButtonItemStyleBordered target:self action:@selector(toggleDividerStyle:)];         
        
        UIBarButtonItem_toggleMasterBeforeDetail = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Vertical",@"Show Controller") style:UIBarButtonItemStyleBordered target:self action:@selector(toggleMasterBeforeDetail:)];    
        
        
        
        [toolbar setItems:[NSArray arrayWithObjects:UIBarButtonItem_toggleMasterView ,UIBarButtonItem_toggleVertical,UIBarButtonItem_toggleDividerStyle,UIBarButtonItem_toggleMasterBeforeDetail,nil] animated:YES];     
        
        
        [dv addSubview:toolbar];
    }
   
    
    //走勢圖
    ccchart=[[KChart alloc] initWithFrame:CGRectMake(0, 44, 100, 100)];    
    [ccchart setBackgroundColor:[UIColor  redColor]];
    [dv addSubview:ccchart];
    [ccchart release];
    
    self.view=dv;
    [dv release];
    
       
}
 
#pragma mark -
#pragma mark Managing the detail item


// When setting the detail item, update the view and dismiss the popover controller if it's showing.
- (void)setDetailItem:(id)newDetailItem
{
    if (detailItem != newDetailItem) {
        [detailItem release];
        detailItem = [newDetailItem retain];
        
        // Update the view.
        [self configureView];
    }
	
         
}


- (void)configureView
{
    // Update the user interface for the detail item.
    detailDescriptionLabel.text = [detailItem description];
	//toggleItem.title = ([splitController isShowingMaster]) ? @"Hide Master" : @"Show Master"; // "I... AM... THE MASTER!" Derek Jacobi. Gave me chills.
	//verticalItem.title = (splitController.vertical) ? @"Horizontal Split" : @"Vertical Split";
	//dividerStyleItem.title = (splitController.dividerStyle == MGSplitViewDividerStyleThin) ? @"Enable Dragging" : @"Disable Dragging";
	//masterBeforeDetailItem.title = (splitController.masterBeforeDetail) ? @"Detail First" : @"Master First";
}


#pragma mark -
#pragma mark Split view support


- (void)splitViewController:(MGSplitViewController*)svc 
	 willHideViewController:(UIViewController *)aViewController 
		  withBarButtonItem:(UIBarButtonItem*)barButtonItem 
	   forPopoverController: (UIPopoverController*)pc
{
	//NSLog(@"%@", NSStringFromSelector(_cmd));
	/*
	if (barButtonItem) {
		barButtonItem.title = @"Popover";
		NSMutableArray *items = [[toolbar items] mutableCopy];
		[items insertObject:barButtonItem atIndex:0];
		[toolbar setItems:items animated:YES];
		[items release];
	}
     */
   // self.popoverController = pc;
}


// Called when the view is shown again in the split view, invalidating the button and popover controller.
- (void)splitViewController:(MGSplitViewController*)svc 
	 willShowViewController:(UIViewController *)aViewController 
  invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
	//NSLog(@"%@", NSStringFromSelector(_cmd));
	
	if (barButtonItem) {
		NSMutableArray *items = [[toolbar items] mutableCopy];
		[items removeObject:barButtonItem];
		[toolbar setItems:items animated:YES];
		[items release];
	}
    //self.popoverController = nil;
}


- (void)splitViewController:(MGSplitViewController*)svc 
		  popoverController:(UIPopoverController*)pc 
  willPresentViewController:(UIViewController *)aViewController
{
	//NSLog(@"%@", NSStringFromSelector(_cmd));
}


- (void)splitViewController:(MGSplitViewController*)svc willChangeSplitOrientationToVertical:(BOOL)isVertical
{
	//NSLog(@"%@", NSStringFromSelector(_cmd));
}


- (void)splitViewController:(MGSplitViewController*)svc willMoveSplitToPosition:(float)position
{
	NSLog(@"通知移動到新位置 MGChartViewController");
}


- (float)splitViewController:(MGSplitViewController *)svc constrainSplitPosition:(float)proposedPosition splitViewSize:(CGSize)viewSize
{
	//NSLog(@"%@", NSStringFromSelector(_cmd));
	return proposedPosition;
}


#pragma mark -
#pragma mark Actions


- (void)toggleMasterView:(id)sender
{
	NSLog(@"toggleMasterView");
    [splitController toggleMasterView:sender];
	[self configureView];
}


- (void)toggleVertical:(id)sender
{
	NSLog(@"toggleVertical");
    [splitController toggleSplitOrientation:self];
	[self configureView];
}


- (void)toggleDividerStyle:(id)sender
{
	NSLog(@"toggleDividerStyle");
    MGSplitViewDividerStyle newStyle = ((splitController.dividerStyle == MGSplitViewDividerStyleThin) ? MGSplitViewDividerStylePaneSplitter : MGSplitViewDividerStyleThin);
    
	[splitController setDividerStyle:newStyle animated:YES];
	
    [self configureView];
}


- (void)toggleMasterBeforeDetail:(id)sender
{
	[splitController toggleMasterBeforeDetail:sender];
	[self configureView];
}


#pragma mark -
#pragma mark Rotation support


// Ensure that the view controller supports rotation and that the split view can therefore show in both portrait and landscape.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
 


- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
	[self configureView];
}


- (void)dealloc
{
      [toolbar release];
    
    [detailItem release];
    [detailDescriptionLabel release];
    [super dealloc];
}
#pragma mark -
#pragma mark 底層框架改變

-(void)set_new_frame:(CGRect)frame{
    NSLog(@"重設 走勢圖框架 set_new_frame");
   
   // detail_frame=frame;
    CGRect chartframe;
    chartframe.size.width=frame.size.width;
    chartframe.size.height=frame.size.height;    
    chartframe.origin.x=0;
    chartframe.origin.y=0;
    
    [ccchart set_cchart_frame:chartframe];

    CGRect toolbarframe;
    toolbarframe.size.width= frame.size.width;
    toolbarframe.size.height=44;    
    toolbarframe.origin.x=0;
    toolbarframe.origin.y=0;    
    [toolbar setFrame:toolbarframe];  
    
    //UIBarButtonItem_toggleMasterView.width=10;
}

 

@end
